package com._520it.wms.service.impl;

import com._520it.wms.domain.OrderBill;
import com._520it.wms.domain.OrderBillItem;
import com._520it.wms.mapper.OrderBillItemMapper;
import com._520it.wms.mapper.OrderBillMapper;
import com._520it.wms.query.PageResult;
import com._520it.wms.query.QueryObject;
import com._520it.wms.service.IOrderBillService;
import com._520it.wms.util.UserContext;
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.ObjectError;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 采购订单管理
 */
@Service
public class OrderBillServiceImpl implements IOrderBillService {
    @Autowired
    private OrderBillMapper orderBillMapper;
    //订单明细
    @Autowired
    private OrderBillItemMapper orderBillItemMapper;

    @Override
    public void saveOrUpdate(OrderBill orderBill) {
        if (orderBill.getId() != null) {
            //修改操作

            //1只有未审核的单据可以编辑
            if (orderBill.getStatus() == orderBill.NORMAL) {
                // 2根据订单id删除订单明细数据
                orderBillItemMapper.deleteByBillId(orderBill.getId());
                //3设置总数量和总金额
                BigDecimal totalAmount = BigDecimal.ZERO;
                BigDecimal totalNumber = BigDecimal.ZERO;
                //设置计算采购总金额和总数量
                //遍历出所有的数据
                for (OrderBillItem orderBillItem : orderBill.getItems()) {
                    BigDecimal number = orderBillItem.getNumber();
                    BigDecimal costPrice = orderBillItem.getCostPrice();
                    //计算金额小计 乘法相乘 保留两位小数 超过一半四舍五入
                    BigDecimal amount = number.multiply(costPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                    orderBillItem.setAmount(amount);
                    totalAmount = totalAmount.add(amount);//定义变量接收返回值
                    totalNumber = totalNumber.add(number);
                    //4保存订单明细(关联订单id)
                    orderBillItem.setBillId(orderBill.getId());
                    orderBillItemMapper.insert(orderBillItem);
                }
                //5保存订单数据
                orderBill.setTotalAmount(totalAmount);
                orderBill.setTotalNumber(totalNumber);
                orderBillMapper.updateByPrimaryKey(orderBill);
            } else {
                throw new RuntimeException("已经审核的单据不能修改");
            }
        } else {
            //新增操作
            //设置录入人,录入时间
            orderBill.setInputUser(UserContext.getCurrentUser());
            orderBill.setInputTime(new Date());
            //单据状态为未审核
            orderBill.setStatus(orderBill.NORMAL);
            BigDecimal totalAmount = BigDecimal.ZERO;
            BigDecimal totalNumber = BigDecimal.ZERO;
            //设置计算采购总金额和总数量
            //遍历出所有的数据
            for (OrderBillItem orderBillItem : orderBill.getItems()) {
                BigDecimal number = orderBillItem.getNumber();
                BigDecimal costPrice = orderBillItem.getCostPrice();
                //计算金额小计 乘法相乘 保留两位小数 超过一半四舍五入
                BigDecimal amount = number.multiply(costPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
                orderBillItem.setAmount(amount);
                totalAmount = totalAmount.add(amount);//定义变量接收返回值
                totalNumber = totalNumber.add(number);
            }
            //设置总金额
            orderBill.setTotalAmount(totalAmount);
            orderBill.setTotalNumber(totalNumber);
            //保存订单数据
            orderBillMapper.insert(orderBill);
            //保存订单明细(关联订单的id)
            for (OrderBillItem item : orderBill.getItems()) {
                //建立关联关系
                item.setBillId(orderBill.getId());
                orderBillItemMapper.insert(item);
            }

        }

    }

    @Override
    public void delete(Long id) {
        OrderBill bill = orderBillMapper.selectByPrimaryKey(id);
        //只有未审核的单据才能删除
        if (OrderBill.NORMAL == bill.getStatus()) {
            //根据id删除明细,然后删除采购订单
            orderBillItemMapper.deleteByBillId(id);
            orderBillMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public OrderBill get(Long id) {
        return orderBillMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int totalCount = orderBillMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize());
        }
        //查询结果集
        List<OrderBill> list = orderBillMapper.queryForList(qo);
        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), list, totalCount);
    }

    @Override
    //采购订单审核
    public void audit(Long id) {
        //判断单据的状态 是未审核的单据操作审核
        OrderBill orderBill = orderBillMapper.selectByPrimaryKey(id);
        if (OrderBill.NORMAL == orderBill.getStatus()) {
            //设置审核时间
            orderBill.setInputTime(new Date());
            //设置审核状态
            orderBill.setStatus(OrderBill.AUDIT);
            //设置审核人
            orderBill.setInputUser(UserContext.getCurrentUser());
            orderBillMapper.audit(orderBill);


        }
    }
}